﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace DuLabs.Classic.Util
{
    public static class XmlSerializationFormat
    {
        public static void ToFile(string path, object obj)
        {
            try
            {
                XmlSerializationFormat.ToFile(path, obj, null);
            }
            catch
            {
                throw;
            }
        }

        public static void ToFile(string path, object obj, XmlSerializerNamespaces namespaces)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            try
            {
                // 如果文件目录不存在则创建目录
                FileInfo fileInfo = new FileInfo(path);
                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }

                if (namespaces == null)
                {
                    namespaces = new XmlSerializerNamespaces();
                    namespaces.Add("", "");
                }                

                XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());

                using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    xmlSerializer.Serialize(fileStream, obj, namespaces);
                }
            }
            catch
            {
                throw;
            }
        }

        public static T ToObject<T>(string path) where T : class, new()
        {
            try
            {                
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

                using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    object obj = xmlSerializer.Deserialize(fileStream);
                    return obj as T;
                }                
            }
            catch
            {
                throw;
            }
        }

        public static T ToObjectByText<T>(string xmlText) where T : class, new()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

                using (MemoryStream memoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlText)))
                {
                    object obj = xmlSerializer.Deserialize(memoryStream);
                    return obj as T;
                }                
            }
            catch
            {
                throw;
            }
        }

        public static string ToText(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    xmlSerializer.Serialize(memoryStream, obj);
                    byte[] bytes = memoryStream.ToArray();
                    return System.Text.Encoding.UTF8.GetString(bytes);
                }
            }
            catch
            {
                throw;
            }
        }        
    }
}
